#include "VirtualKeyboard.h"
#include "UIElement.h"
#include "UICollection.h"
#include "MenuItem.h"
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <string>
#include <vector>
using namespace std;

namespace VirtualKeyboard{
	class KbKey : public UI::UIElement{
		public:
			KbKey(int x, int y, char text){
				this->x = x;
				this->y = y;
				this->Letter = text;
				Selected = false;
			}
			void Draw(){
				//Moves to y, x coordinates
				if (Selected == true)
				{
					printf("\x1b[%i;%iH", y, x-1);
					printf("[%c]", Letter);
				}
				else
				{
					printf("\x1b[%i;%iH", y, x-1);
					printf(" %c ", Letter);
				}
				Selected = false;
			}
			bool Selected;
			char Letter;
		private:
			int x;
			int y;
	};
	
	UI::UICollection* KeysCollection = new UI::UICollection;
	vector<KbKey*> KeysDirect;
	
	void AddKeyboardRow(int StartX, int StartY, vector<char> KeyList){
		unsigned int i;
		KbKey* newKey;
		for (i=0; i<KeyList.size(); i++)
		{
			newKey = new KbKey(StartX, StartY, KeyList.at(i));
			KeysDirect.push_back(newKey);
			KeysCollection->add(KeysDirect.back());
			StartX += 3;
		}
	}
	
	std::string ShowKeyboard(string DefaultRet){
		KeysCollection = new UI::UICollection;
		KeysDirect.erase(KeysDirect.begin(), KeysDirect.end());
		int xat = 22;
		int yat = 12;
		
		string RetVal = DefaultRet;
		
		const int n = 13;
		
		
		vector<char> KeyList1;
		vector<char> KeyList2;
		vector<char> KeyList3;
		vector<char> KeyList4;
		vector<char> KeyList5;
		vector<char> KeyList6;
		
		char CharList1[n] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '-', '+'};
		char CharList2[n] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'};
		char CharList3[n] = {'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
		char CharList4[n] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'};
		char CharList5[n] = {'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
		char CharList6[n] = {' ', '(', ')', '[', ']', '<', '>', '.', '!', '?', '~', '\'', ':'};
		//char CharListN[n] = {'', '', '', '', '', '', '', '', '', '', '', '', ''};
		copy ( CharList1, CharList1 + n, back_inserter ( KeyList1 ) );
		copy ( CharList2, CharList2 + n, back_inserter ( KeyList2 ) );
		copy ( CharList3, CharList3 + n, back_inserter ( KeyList3 ) );
		copy ( CharList4, CharList4 + n, back_inserter ( KeyList4 ) );
		copy ( CharList5, CharList5 + n, back_inserter ( KeyList5 ) );
		copy ( CharList6, CharList6 + n, back_inserter ( KeyList6 ) );
		
		AddKeyboardRow(xat, yat, KeyList1 );
		yat++;
		AddKeyboardRow(xat, yat, KeyList2 );
		yat++;
		AddKeyboardRow(xat, yat, KeyList3 );
		yat++;
		AddKeyboardRow(xat, yat, KeyList4 );
		yat++;
		AddKeyboardRow(xat, yat, KeyList5 );
		yat++;
		AddKeyboardRow(xat, yat, KeyList6 );
		yat++;
				
		u32 pressed;
		unsigned int Selected = 0;
		
		UI::MenuItem* VirtualKeyboardTop = new UI::MenuItem(30,10,"-= Virtual Keyboard =-");
		UI::MenuItem* VirtualKeyboardBack = new UI::MenuItem(21,18,"[Backspace]: (b)");
		UI::MenuItem* VirtualKeyboardSpace= new UI::MenuItem(21,19,"[Space]: (x)");
		UI::MenuItem* VirtualKeyboardEnd = new UI::MenuItem(21,20,"[Done]: (+)");
		UI::MenuItem* VirtualKeyboardRetValBlank = new UI::MenuItem(29,11,"                      ");
		UI::MenuItem* VirtualKeyboardRetVal = new UI::MenuItem(30,11,RetVal);
		KeysCollection->add(VirtualKeyboardTop);
		KeysCollection->add(VirtualKeyboardBack);
		KeysCollection->add(VirtualKeyboardSpace);
		KeysCollection->add(VirtualKeyboardEnd);
		KeysCollection->add(VirtualKeyboardRetVal);
		
		bool Looping = true;
		while (Looping){
			VirtualKeyboardRetVal->text.erase();
			VirtualKeyboardRetVal->text.assign(RetVal+"| ");
			WPAD_ScanPads();
			pressed = WPAD_ButtonsDown(0);
	
			unsigned int Min = 0;
			unsigned int Max = (n*6)-1;
			
			if (pressed&WPAD_CLASSIC_BUTTON_PLUS) {
				Looping = false;
			}
			
			if (pressed&WPAD_CLASSIC_BUTTON_A){
				if (RetVal.size() < 20) {
					RetVal.push_back(KeysDirect.at(Selected)->Letter);
				}
			}
			
			if (pressed&WPAD_CLASSIC_BUTTON_X){
				if (RetVal.size() < 20) {
					RetVal.push_back(' ');
				}
			}
			
			if (pressed&WPAD_CLASSIC_BUTTON_B){
				VirtualKeyboardRetValBlank->Draw();
				if (Selected != n*6 && RetVal.size() > 0){
					RetVal.erase(RetVal.size()-1);
				}
			}
			
			//These will properly Wrap the keys
			if (pressed&WPAD_CLASSIC_BUTTON_UP) Selected-=n;
			if (pressed&WPAD_CLASSIC_BUTTON_DOWN) Selected+=n;
			
			
			if (Selected > Max) Selected =  (Selected%Max)-1;
			if (Selected < Min) Selected = Selected + Max + 1;
			
			
			if (pressed&WPAD_CLASSIC_BUTTON_RIGHT){
				Selected++;			
				if (Selected%n == 0) Selected -= n;
			}
			
			if (pressed&WPAD_CLASSIC_BUTTON_LEFT) {
				Selected--;
				if ((Selected+1)%n == 0) Selected += n;
			}
			
			if (Selected < KeysDirect.size()) KeysDirect.at(Selected)->Selected = true;
			if (Selected >= KeysDirect.size()) VirtualKeyboardEnd->Selected = true;
			KeysCollection->RedrawCollection();
		}
	return RetVal;
	}
}


